<!--
 * @Description: Transfer
 * @Author: wangyan
 * @Date: 2021-03-09 16:58:45
 * @LastEditTime: 2021-03-30 14:33:28
 * @LastEditors: @yzcheng
-->
<!--  -->
<template>
  <el-dialog
    v-model="innerVisible"
    :close-on-click-modal="false"
    :show-close="false"
    width="500px"
  >
    <el-form
      :model="formInline"
      class="demo-form-inline"
      v-if="fromType === 2"
      :rules="rules"
      label-width="80px"
      ref="ruleForm"
    >
      <el-form-item label="当前节点">
        {{ data.name }}
      </el-form-item>
      <el-form-item :label="label" prop="userCode">
        <el-select
          v-model="formInline.userCode"
          :placeholder="'请选择' + label"
        >
          <el-option
            v-for="(item, index) in userList.data"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="原因">
        <el-input v-model="formInline.desc" width="120px"></el-input>
      </el-form-item>
    </el-form>
    <el-form
      :model="formInline"
      class="demo-form-inline"
      v-if="fromType === 1"
      ref="ruleForm"
    >
      <el-form-item :label="label">
        <el-input type="textarea" v-model="formInline.desc"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cl">取 消</el-button>
        <el-button type="primary" @click="enter('ruleForm')">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { mapActions } from "vuex";
export default {
  name: "",
  data() {
    return {
      formInline: {
        userCode: "",
        desc: "",
      },
      label: "",
      title: "",
      userList: [],
      rules: {
        userCode: [{ required: true, message: "请选择", trigger: "change" }],
      },
    };
  },
  props: {
    innerVisible: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    type: String,
    data: Object,
    closeFn: Function,
    fromType: Number, // 1 是文本 2是下拉选择人员
    Generalrequest: Function,
    searchTodoList: Function,
    userCodeList: Array
  },
  methods: {
    ...mapActions("flowDesignStore", ["getUserList"]),
    ...mapActions("todoListStore", ["saveCopies"]),
    async getuser() {
      this.userList = await this.getUserList();
    },
    cl() {
      this.closeFn();
      this.formInline = {};
    },
    enter(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          var data;
          if (this.type === "reject") {
            data = new FormData();
            data.append("currentTaskId", this.data.id);
            data.append("comment", this.formInline.desc);
            // data.currentTaskId = this.data.id;
            // data.comment = this.formInline.desc;
          }
          if (this.type === "delegateTask" || this.type === "transfer") {
            data = {
              taskId: this.data.id,
              procInsId: this.data.processIntanceId,
              comment: this.formInline.desc,
              userCode: this.formInline.userCode,
            };
          } else if (this.type === "FinishTask") {
            data = {
              taskId: this.data.id,
              procInsId: this.data.processIntanceId,
              comment: this.formInline.desc,
              // currentTaskId: this.data.id,
            };
            this.saveCopies({
              processInstanceId: this.data.processIntanceId,
              taskId: this.data.id,
              userCodeList: this.userCodeList,
              processDefinitionId: this.data.processDefinitionId
            })
          } else if (this.type === "abandoned") {
            data = {
              processInstanceId: this.data.processIntanceId,
              comment: this.formInline.desc,
            };
          } else if (this.type === "rejectLast") {
            data = new FormData();
            data.append("currentTaskId", this.data.id);
            data.append("comment", this.formInline.desc);
          } else if (this.type === "spreject") {
            data = new FormData();
            data.append("currentTaskId", this.data.id);
            data.append("comment", this.formInline.desc);
          }

          await this.Generalrequest(data);

          this.closeFn();
          setTimeout(() => {
            this.searchTodoList({
              pageOption: { pageNum: 1, pageSize: 10 },
            });
          }, 300);

          this.formInline = {};
        } else {
          return false;
        }
      });
    },
  },
  watch: {
    type() {
      switch (this.type) {
        case "reject":
          this.label = 1;
          this.label = "退回原因";

          break;
        case "rejectLast":
          this.label = 1;
          this.label = "退回原因";

          break;
        case "spreject":
          this.label = 1;
          this.label = "驳回原因";

          break;
        case "delegateTask":
          this.label = 2;
          this.label = "委派人";

          break;
        case "transfer":
          this.label = 2;
          this.label = "转办人";

          break;
        case "FinishTask":
          console.log(this);
          this.label = "通过原因";
          break;
        default:
          break;
      }
    },
  },

  //生命周期 - 创建完成（访问当前this实例）
  // created() {},
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.getuser();
  },
};
</script>
<style scoped lang='scss'>
/* @import url(); 引入scss类 */
</style>
